package com.hk.housekeeping.controller;

import com.hk.housekeeping.entity.Staff;
import com.hk.housekeeping.service.StaffServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffServ staffServ;

    //增添一条记录
    @PostMapping("/add")
    public ResponseEntity<Integer> addStaff(@RequestBody Staff staff) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = staffServ.addStaff(staff);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_ADD_ERROR);
        }
        return entity;
    }

    //删除一条记录
    @DeleteMapping("/del")
    public ResponseEntity<Integer> delStaff(int id) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = staffServ.delStaff(id);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_DEL_ERROR);
        }
        return entity;
    }

    //信息回显（返回一条员工记录，不包含荣誉表）
    @GetMapping("/getStaffInfo")
    public ResponseEntity<Staff> selectStaff(int id) {
        ResponseEntity<Staff> entity = null;
        try {
            entity = staffServ.selectStaff(id);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_SELECT_ERROR);
        }

        return entity;
    }

    //信息回显（返回一条员工记录，包含荣誉表）
    @GetMapping("/getStaffInfoHonor")
    public ResponseEntity<Staff> selectStaffHonor(int id) {
        ResponseEntity<Staff> entity = null;
        try {
            entity = staffServ.selectStaffHonor(id);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_SELECT_ERROR);
        }
        return  entity;
    }

    //编译员工信息（先删除，再插入数据）
    @PutMapping("/edit")
    public ResponseEntity<Integer> editStaff(@RequestBody Staff staff) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = staffServ.editStaff(staff);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_EDIT_ERROR);
        }
        return entity;
    }

    //分页查询
    @GetMapping("/pageSelect")
    public ResponseEntity<PageUtils<List<Staff>>> pageSelect(@RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<Staff>>> entity = null;
        try {
            entity = staffServ.pageSelect(currentPage, pageSize);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.STAFF_PAGE_ERROR);
        }

        return entity;
    }

    //联合查询（通过员工姓名和手机号进行查询）
    @GetMapping("/searchByDouble")
    public ResponseEntity<PageUtils<List<Staff>>> selectStaffByNameAndPhone(String name, String phone, @RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int page) {
        ResponseEntity<PageUtils<List<Staff>>> entity = null;

        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(phone)) {
            entity = ResponseEntity.error(ResponseCode.STAFF_SEARCH_ERROR);
        }  else if (StringUtils.isEmpty(phone) && !StringUtils.isEmpty(name)) {
            //手机号为空，名字不为空，按名字查询
            entity = staffServ.selectStaffByName(name, currentPage, page);
        } else if (!StringUtils.isEmpty(phone)) {
            //名字为空，手机号不为空，按手机号查询
            //两者都不为空，那么就按照手机号查询，因为手机号是唯一的
            entity = staffServ.selectStaffByPhone(phone);
        }

        return entity;
    }
}
